//
//  LoginViewController.m
//  QuickCure37
//
//  Created by 马文星 on 16/6/17.
//  Copyright © 2016年 Demos. All rights reserved.
//

#import "LoginViewController.h"
#import "LeftViewController.h"

@interface LoginViewController ()

@property (nonatomic, strong) UIImageView * logoView;
@property (nonatomic, strong) UIImageView * logoLabel;
@property (nonatomic, strong) UITextField * accountTF;
@property (nonatomic, strong) UITextField * passwordTF;
@property (nonatomic, strong) UIButton    * loginButton;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupUI];
}

-(void)setupUI{
    
    [self.view addSubview:self.logoView];
    [self.view addSubview:self.logoLabel];
    [self.view addSubview:self.passwordTF];
    [self.view addSubview:self.accountTF];
    [self.view addSubview:self.loginButton];
    
    //布局logo图片
    [self.logoView mas_makeConstraints:^(MASConstraintMaker *make) {
    
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).mas_offset(200);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(90);
    }];
    
    //布局logo文字
    [self.logoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.logoView.mas_bottom);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(45);
        
    }];
    
    //布局用户ID
    [self.accountTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.logoLabel.mas_bottom).mas_offset(20);
        make.width.mas_equalTo(SCREEN_WIDTH - 80);

    }];
    
    //布局密码
    [self.passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.accountTF.mas_bottom).mas_offset(20);
        make.width.mas_equalTo(SCREEN_WIDTH - 80);

    }];
    
    //布局登录按钮
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.passwordTF.mas_bottom).mas_offset(20);
        make.width.mas_equalTo(SCREEN_WIDTH - 80);
        

    }];
    
}

#pragma mark
#pragma mark - logo图片
-(UIImageView *)logoView{
    
    if (_logoView == nil) {
        
        _logoView = [[UIImageView alloc]init];
        
        _logoView.image = [UIImage imageNamed:@"product_img_logo_nor.png"];
        
    }
    return _logoView;
    
}

#pragma mark
#pragma mark - logo文字
-(UIImageView *)logoLabel{
    if (_logoLabel == nil) {
        _logoLabel = [[UIImageView alloc]init];
        
        _logoLabel.image = [UIImage imageNamed:@"product_text_logo_nor.png"];
    }
    return _logoLabel;
}

#pragma mark
#pragma mark - userid
-(UITextField *)accountTF{
    
    if (_accountTF == nil) {
        //创建UITextField
        _accountTF = [[UITextField alloc]init];
        _accountTF.placeholder=@"请输入用户ID";
        //取消符号'×'
        _accountTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        //边框
        _accountTF.borderStyle = UITextBorderStyleRoundedRect;
    }
    
    return _accountTF;
}

#pragma mark
#pragma mark - 密码
-(UITextField *)passwordTF{
    if (_passwordTF == nil) {
        
        _passwordTF = [[UITextField alloc] init];
        _passwordTF.placeholder = @"请输入密码";
        _passwordTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _passwordTF.borderStyle =UITextBorderStyleRoundedRect;
    }
    return _passwordTF;
}

#pragma mark
#pragma mark - 按钮
-(UIButton *)loginButton{
    
    if (_loginButton == nil) {
        
        _loginButton = [[UIButton alloc] init];
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
    
        _loginButton.tintColor = [UIColor whiteColor];
        _loginButton.backgroundColor = kThemeColor;
        
        [_loginButton addTarget:self action:@selector(clickLoginBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _loginButton;
}

#pragma mark
#pragma mark - 点击登录按钮
- (void)clickLoginBtn{
    
    //获取用户ID和密码
    NSString *userID = self.accountTF.text;
    NSString *pwd = self.passwordTF.text;
    
    //请求网络数据
    [self getDataWithUserID:userID withPassword:pwd];
}

#pragma mark
#pragma mark - 请求数据
- (void)getDataWithUserID:(NSString *)userID withPassword:(NSString *)pwd{
 
    //获取管理器 AFHTTPRequestOperationManager是以前的写法
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
 
    //可接受的数据格式
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    
    //更改默认请求的发送请求的二进制数据为JSON格式的二进制更改默  认的序列化方式
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    //设置请求体参数
    NSDictionary *dict = @{@"login_token":@"985c96ce43bb5ef69de3e1ad8afaa83f"};
    
    //post请求
    [manager POST:@"http://iosapi.itcast.cn/carelinkQuickLogin.json.php" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //判断返回的是否有值
        if (responseObject) {
            
            // 先把菊花移除掉
            [MBProgressHUD removeAllHudForWindow];
            
            [MBProgressHUD showSuccessMessage:@"登录成功"];
            
            // 写入文件
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            
            [userDefaults setValue:@"123" forKey:@"pwd"];
            
            [userDefaults synchronize];
            
            //刷新
            LeftViewController *leftVC = [LeftViewController new];
            
            self.sidePanelController.leftPanel = leftVC;
            
            //回到上一级
            [self.navigationController popViewControllerAnimated:YES];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}

@end
